package vn.com.vega.music.view.adapter;

import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.objects.Song;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CurrentPlaylistAdapter extends BaseAdapter {
	List<Song> mListSongs;
	int positionPlay = -1;
	LayoutInflater inflater;

	public CurrentPlaylistAdapter(List<Song> list, int position, Context ctx) {
		mListSongs = list;
		positionPlay = position;
		inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setPositionPlay(int position) {
		if (position != positionPlay) {
			positionPlay = position;
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListSongs.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListSongs.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.playlist_now_playing_item,
					null);
			holder = new Holder();
			holder.markPlay = (ImageView) convertView
					.findViewById(R.id.imv_mark_song_current_play);
			holder.songTitle = (TextView) convertView
					.findViewById(R.id.tv_song_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		Song song = (Song) getItem(position);
		holder.songTitle.setText(song.title + " - " + song.artist_name);
		if (positionPlay == position) {
			holder.markPlay.setVisibility(View.VISIBLE);
		} else {
			holder.markPlay.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	static class Holder {
		ImageView markPlay;
		TextView songTitle;
	}

}
